package com.beta.user.auth.center.service.provider;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.beta.user.auth.center.api.model.SelfappAuthAcc;
import com.beta.user.auth.center.api.request.AddSelfAppAuthAccReq;
import com.beta.user.auth.center.api.request.QuerySelfAppAuthAccReq;
import com.beta.user.auth.center.api.request.UpdateSelfAppAuthAccReq;
import com.beta.user.auth.center.api.response.AddSelfAppAuthAccResp;
import com.beta.user.auth.center.api.response.CommonResultEnum;
import com.beta.user.auth.center.api.response.QuerySelfAppAuthAccResp;
import com.beta.user.auth.center.api.response.UpdateSelfAppAuthAccResp;
import com.beta.user.auth.center.api.service.IRPCSelfAppAuthAccService;
import com.beta.user.auth.center.service.mapper.model.SelfappAuthAccount;
import com.beta.user.auth.center.service.service.ISelfappAuthAccService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author gwqin
 * @description
 * @date 2023-07-17 11:46
 */
@Slf4j
@Service
@DubboService
public class RPCSelfAppAuthAccServiceImpl implements IRPCSelfAppAuthAccService {

    @Autowired
    private ISelfappAuthAccService accountService;

    @Override
    public AddSelfAppAuthAccResp addSelfAppAuthAcc(AddSelfAppAuthAccReq req) {
        AddSelfAppAuthAccResp resp = new AddSelfAppAuthAccResp();
        SelfappAuthAcc authAcc = req.getSelfAppAuthAcc();
        if (authAcc == null
                || StringUtils.isEmpty(authAcc.getAppId())
                || StringUtils.isEmpty(authAcc.getUserId())) {
            log.warn("RPCSelfAppAuthAccServiceImpl addSelfAppAuthAcc 参数非法，req = {}", JSON.toJSONString(req));
            resp.setResult(CommonResultEnum.MISSING_REQUEST_PARAMETER);
            return resp;
        }

        SelfappAuthAccount authAccount = new SelfappAuthAccount();
        BeanUtils.copyProperties(authAcc, authAccount);
        accountService.insertIgnore(authAccount);
        return resp;
    }

    @Override
    public QuerySelfAppAuthAccResp querySelfAppAuthAcc(QuerySelfAppAuthAccReq req) {
        QuerySelfAppAuthAccResp resp = new QuerySelfAppAuthAccResp();
        String appId = req.getAppId();
        String userId = req.getUserId();
        Long id = req.getId();
        if (StringUtils.isEmpty(appId)
                || (StringUtils.isEmpty(userId) && id == null)) {
            log.warn("RPCSelfAppAuthAccServiceImpl querySelfAppAuthAcc 参数非法，req = {}", JSON.toJSONString(req));
            resp.setResult(CommonResultEnum.MISSING_REQUEST_PARAMETER);
            return resp;
        }

        LambdaQueryWrapper<SelfappAuthAccount> queryWrapper = new LambdaQueryWrapper<SelfappAuthAccount>()
                .eq(id != null, SelfappAuthAccount::getId, id)
                .eq(StringUtils.isEmpty(appId), SelfappAuthAccount::getAppId, appId)
                .eq(StringUtils.isNotBlank(userId), SelfappAuthAccount::getUserId, userId)
                .orderByDesc(SelfappAuthAccount::getId)
                .last(" LIMIT 1");

        SelfappAuthAccount authAccount = accountService.getOne(queryWrapper);
        if (authAccount == null) {
            log.warn("RPCSelfAppAuthAccServiceImpl querySelfAppAuthAcc 未查到数据，req = {}", JSON.toJSONString(req));
            return resp;
        }

        SelfappAuthAcc appAuthAcc = new SelfappAuthAcc();
        BeanUtils.copyProperties(authAccount, appAuthAcc);
        resp.setSelfAppAuthAcc(appAuthAcc);

        return resp;
    }

    @Override
    public UpdateSelfAppAuthAccResp updateSelfAppAuthAcc(UpdateSelfAppAuthAccReq req) {
        UpdateSelfAppAuthAccResp resp = new UpdateSelfAppAuthAccResp();
        SelfappAuthAcc authAcc = req.getSelfAppAuthAcc();
        if (authAcc == null
                || StringUtils.isEmpty(authAcc.getAppId())
                || (StringUtils.isEmpty(authAcc.getUserId()) && authAcc.getId() == null)) {
            log.warn("RPCSelfAppAuthAccServiceImpl updateSelfAppAuthAcc 参数非法，req = {}", JSON.toJSONString(req));
            resp.setResult(CommonResultEnum.MISSING_REQUEST_PARAMETER);
            return resp;
        }
        SelfappAuthAccount authAccount = new SelfappAuthAccount();
        BeanUtils.copyProperties(authAcc, authAccount);
        accountService.updateById(authAccount);
        return resp;
    }
}
